import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from 'view/user/login';
import Homelayout from '../layout/Homelayout'
import NotFound from "view/404"
import Welcome from "view/welcome"
// import Users from "../views/users"
import Users from "view/users"
import Roles from "view/roles"
import Rights from "view/rights"
import Params from "view/params"
import Goods from "view/goods"
import store from 'store'

Vue.use(VueRouter)

const routes = [
  {
    path: "/login",
    name: "login",
    component: Login
  },
  {
    path: "/",
    name: "Homelayout",
    component: Homelayout,
    redirect: "/welcome",
    children: [
      {
        path: "/users",
        name: "users",
        component: Users
      },
      {
        path: "/roles",
        name: "roles",
        component: Roles
      },
      {
        path: "/rights",
        name: "rights",
        component: Rights 
      },
      {
        path: "/params",
        name: "params",
        component:Params
      },
      {
        path: "/goods",
        name: "goods",
        component:Goods
      },
      {
        path: "/welcome",
        name: "welcome",
        component: Welcome
      },
      {
        path: "*",
        name: "404",
        component: NotFound
      },
      
      
    ]
  },


]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});



router.beforeEach((to, from, next) => {
  const token = store.getters.token;
  const isLogin = to.name === 'login';
  // 如果有token 并且在登录页 那么就跳转页面
  if (token && isLogin) {
    return next({ name: "homelayout" });
  }
  // 如果没有token 并且不是在登录页 那么久跳转到登录页
  if (!token && !isLogin) {
    return next({ name: "login" })
  }
  // 其他正常
  next();
})

export default router
